//
//  GSCommonLoginSuccessView.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/30.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginSuccessView.h"

@interface GSCommonLoginSuccessView () {
    NSInteger _time;
}

@property (nonatomic, assign) GSCommonLoginSuccessType successType;

/**
 *backTimeLabel
 */
@property (nonatomic, strong) UILabel *backTimeLabel;

@end

@implementation GSCommonLoginSuccessView

- (instancetype)initWithSuccessType:(GSCommonLoginSuccessType)successType
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = true;
        self.successType = successType;
        _time = 3;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    CGFloat topPadding = 36.0;
    UILabel *titleLb = [UILabel new];
    titleLb.textColor = GSLOGINRGBRGBHEX(0x000000);
    titleLb.font = [UIFont systemFontOfSize:28 weight:UIFontWeightRegular];
    [self addSubview:titleLb];
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(topPadding);
        make.centerX.mas_equalTo(self);
    }];
    titleLb.text = self.successType==GSCommonLoginSuccessTypeRegisterSuccuss?GSLoginLocalized(@"Register Success"):GSLoginLocalized(@"Reset Password Success");
    //
    CGFloat lbTickPadding = 109.0/2;
    UIImageView *tickIV = [UIImageView new];
    tickIV.image = [UIImage imageNamed:@"gslogin_success_tick"];
    [self addSubview:tickIV];
    [tickIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLb.mas_bottom).offset(lbTickPadding);
        make.centerX.mas_equalTo(self);
    }];
    //
    CGFloat tickLbPadding = 48.0/2;
    UILabel *backLabel = [UILabel new];
    backLabel.textColor = GSLOGINRGBRGBHEX(0x666666);
    backLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tickIV.mas_bottom).offset(tickLbPadding);
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-145.0/2);
    }];
    self.backTimeLabel = backLabel;
    [self updateTime];
}

- (void)countdownAction {
    if (_time==0) {
        if (self.countdownComplete) {
            self.countdownComplete();
        }
        return;
    }
    [self performSelector:@selector(countdownAction) withObject:nil afterDelay:1.0];
    [self updateTime];
    _time --;
}

- (void)updateTime {
    NSString *string = GSLoginLocalizedFormatString(@"Return to the login page after %@ seconds",@(_time));
    self.backTimeLabel.text = string;
}

@end
